package utils;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;


public class Utils {

	private final static String set = "set";
	private final static String get = "get";
	
	
	public static <T> T mapeo(Object origen, Class<T> destino) {
		try {

			Class<T> dest = destino;
			Class<?> ori = origen.getClass();

			System.out.println(dest.getName());
			System.out.println(ori.getName());

			Method[] metodos = ori.getDeclaredMethods();
			Method[] metodosdes = dest.getDeclaredMethods();

			T des = dest.newInstance();

			for (int i = 0; i < metodos.length; i++) {
				if (metodos[i].getName().substring(0, 3).equals(get)) {
					Method mthdor = ori.getDeclaredMethod(metodos[i].getName(), metodos[i].getParameterTypes());

					for (int j = 0; j < metodosdes.length; j++) {
						Method mthd = null;

						if (metodosdes[j].getName().equals(set + metodos[i].getName().substring(3)))
							mthd = dest.getMethod(metodosdes[j].getName(), metodosdes[j].getParameterTypes());

						if (mthd != null) {

							if (metodosdes[j].getName().indexOf("fecha") != -1 || metodosdes[j].getName().indexOf("Fecha") != -1) {

								if (dest.getName().indexOf("VO") != -1 && ori.getName().indexOf("VO") == -1) {

									Date date = (Date) mthdor.invoke(origen, new Object[] {});
									SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

									mthd.invoke(des, format.format(date));

								} else if(ori.getName().indexOf("VO") != -1 && dest.getName().indexOf("VO") == -1){
									String date = (String) mthdor.invoke(origen, new Object[] {});
									SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

									mthd.invoke(des, new Date(format.parse(date).getTime()));
								}else
									mthd.invoke(des, mthdor.invoke(origen, new Object[] {}));
							} else
								mthd.invoke(des, mthdor.invoke(origen, new Object[] {}));
						}
					}
				}
			}
			return des;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static  <T> T mapeo(Object origen, T des) {
		try {

			Class<?> dest = des.getClass();
			Class<?> ori = origen.getClass();

			System.out.println(dest.getName());
			System.out.println(ori.getName());

			Method[] metodos = ori.getDeclaredMethods();
			Method[] metodosdes = dest.getDeclaredMethods();
			
			

			for (int i = 0; i < metodos.length; i++) {
				if (metodos[i].getName().substring(0, 3).equals(get)) {
					Method mthdor = ori.getDeclaredMethod(metodos[i].getName(), metodos[i].getParameterTypes());

					for (int j = 0; j < metodosdes.length; j++) {
						Method mthd = null;

						if (metodosdes[j].getName().equals(set + metodos[i].getName().substring(3)))
							mthd = dest.getMethod(metodosdes[j].getName(), metodosdes[j].getParameterTypes());

						if (mthd != null) {

							if (metodosdes[j].getName().indexOf("fecha") != -1 || metodosdes[j].getName().indexOf("Fecha") != -1) {

								if (dest.getName().indexOf("VO") != -1) {

									Date date = (Date) mthdor.invoke(origen, new Object[] {});
									SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

									mthd.invoke(des, format.format(date));

								} else {
									String date = (String) mthdor.invoke(origen, new Object[] {});
									SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

									mthd.invoke(des, new Date(format.parse(date).getTime()));
								}
								
							} else
								mthd.invoke(des, mthdor.invoke(origen, new Object[] {}));
						}
					}
				}
			}
			return des;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static <T> Collection<T> mapeoCollection(Collection<?> origen, Class<T> destino) {
		Collection<T> result = new ArrayList<T>();
		for (Object ob : origen)
			result.add(mapeo(ob, destino));
		return result;
	}
	
	public static String getMethodName()
	  {
	    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
	    return ste[2].getMethodName();
	  }

}




